//
//  FYPush.m
//  TestNotification
//
//  Created by Sean on 15/8/18.
//  Copyright (c) 2015年 FeiYu. All rights reserved.
//

#import "FYPush.h"

#define FY_NOTIFICATIONS @"FY_NOTIFICATIONS"

@implementation FYPush

//设置通知
+ (void)setNotification:(NSArray*)config {
    
    //查看已有通知
//    NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
//    NSLog(@"---notificaitons---%@", notificaitons);
    
    //清除通知
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
//    NSLog(@"---通知已清除---");
    
    //从配置获取通知内容
    if (config == nil || config.count == 0) {
        return;
    }
//    NSLog(@"---config---%@", config);
    
    //注册通知
    [FYPush registNotification];
    
    //初始化通知
    for (NSDictionary *dic in config) {
        [FYPush initNotification:dic];
    }
}

//注册通知
+ (void)registNotification {
    float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
    if(sysVer < 8){
        [self registerPush];
    } else{
        [self registerPushForIOS8];
    }
}

//初始化通知
+ (void)initNotification:(NSDictionary*)dic {
    
    if (dic == nil) {
        return;
    }
//    NSString *key = dic[@"key"];
//    NSString *value = dic[@"value"];
    NSString *time = dic[@"time"];
    NSString *body = dic[@"body"];
//    if (key == nil || key.length == 0) {
//        return;
//    }
//    if (value == nil || value.length == 0) {
//        return;
//    }
    if (time == nil || time.length == 0) {
        return;
    }
    if (body == nil || body.length == 0) {
        return;
    }
    
    //开启通知
    UILocalNotification *notification=[[UILocalNotification alloc] init];
    //设置触发时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSDate *now=[formatter dateFromString:time];
    notification.fireDate= now;
    //设置时区
    notification.timeZone = [NSTimeZone defaultTimeZone];
    //设置重复的时间间隔
    notification.repeatInterval = NSDayCalendarUnit;
    //设置提示内容
    notification.alertBody = body;
    //设置默认的提示声音
    notification.soundName = UILocalNotificationDefaultSoundName;
//    // 设定通知的userInfo，用来标识该通知
//    NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
//    userInfo[key] = value;
    // 设置应用程序右上角的提醒个数
    notification.applicationIconBadgeNumber++;
    // 将通知添加到系统中
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

//获取某个通知
+ (UILocalNotification*)loadNotification:(NSString*)key value:(NSString*)value {
    NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
    //获取当前所有的本地通知
    if (!notificaitons || notificaitons.count <= 0) {
        return nil;
    }
    for (UILocalNotification *notify in notificaitons) {
        if ([[notify.userInfo objectForKey:key] isEqualToString:value]) {
            
            return notify;
        }
    }
    return nil;
}

//取消某一个通知
+ (void)clearNotification {
    NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
    //获取当前所有的本地通知
    if (!notificaitons || notificaitons.count <= 0) {
        return;
    }
    for (UILocalNotification *notify in notificaitons) {
        if ([[notify.userInfo objectForKey:@"myid"] isEqualToString:@"LocalNotificationID"]) {
            //取消一个特定的通知
            [[UIApplication sharedApplication] cancelLocalNotification:notify];
            break;
        }
    }
}

+ (void)registerPushForIOS8{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
    
    //Types
    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    
    //Actions
    UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
    
    acceptAction.identifier = @"ACCEPT_IDENTIFIER";
    acceptAction.title = @"Accept";
    
    acceptAction.activationMode = UIUserNotificationActivationModeForeground;
    acceptAction.destructive = NO;
    acceptAction.authenticationRequired = NO;
    
    //Categories
    UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
    
    inviteCategory.identifier = @"INVITE_CATEGORY";
    
    [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];
    
    [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextMinimal];
    
    NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];
    
    
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
    
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    
    
    [[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
}

+ (void)registerPush {
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}

@end
